<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>component</title>
  <script src="/vue.js"></script>
</head>
<body>
  <div id="root">
    <!-- 父传子：定义属性 -->
    <my-header :num="title" @my-event="handleMyEvent">
      <div>hello</div>
    </my-header>
  </div>
  <script>
    Vue.component('my-header', {
      // 定义props
      props: ['num'],
      template: `
        <div>
          <div v-html="'<h1>'+ title + num +'</h1>'"></div>
          <div>{{title2}}</div>
          <slot></slot>
        </div>
      `,

      data() {
        return {
          title: '这是一个标题'
        }
      },

      computed: {
        title2() {
          return this.title + '<br/><h2>这是副标题</h2>'
        }
      },

      mounted() {
        // 子传父 $emit
        this.$emit('my-event', this.title)
      },

    })

    var vm = new Vue({
      el: '#root',
      data: {
        title: '三'
      },
      methods: {
        handleMyEvent(args) {
          console.log(args)
        }
      },
    })
  </script>
</body>
</html>